/**
 * Project: complex.simple.spring.web
 * 
 * File Created at 2010-3-25
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.ymkyve.complex.simple.spring.web;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.ymkyve.complex.api.ILoginService;


/**
 * LoginController
 * @author wb_liang.gao
 *
 */
public class LoginController implements Controller{
    
    private ILoginService loginService;
    
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	
    	HttpSession session = request.getSession();
    	String a =(String)session.getAttribute("aaa");
    	Map<String,String> map = SessionMap.getMap();
    	if(a==null) {
    		session = request.getSession(true);
    	}
    	session.setMaxInactiveInterval(10);
    	session.setAttribute("aaa", "a");
    	String path  = request.getContextPath();
    	String serverName = request.getServerName();
        if(loginService.doLogin("", "")){
            
            setCookie(response, "sai11a", "ali","ymkyve.com");
            
            //登陆成功
            return new ModelAndView("/views/login/login.html");
        }
        return new ModelAndView("/views/error.vm");
    }

    public ILoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(ILoginService loginService) {
        this.loginService = loginService;
    }
    
    void setCookie(HttpServletResponse response, String name, String value,String domain) {
        Cookie cookie = new Cookie(name, value);
        //设置60秒生存期，如果设置为负值的话，则为浏览器进程Cookie(内存中保存)，关闭浏览器就失效。
        cookie.setMaxAge(60000);
        //设置Cookie路径，不设置的话为当前路径(对于Servlet来说为request.getContextPath() + web.xml里配置的该Servlet的url-pattern路径部分)
        
        //cookie.setPath(path);
        if (domain != null) {
            cookie.setDomain(domain);
        }
        
        
        response.addCookie(cookie);
    }
    
}
